package com.activity;

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.NumberPicker;

public class NumberPickerActivity extends Activity {

	/*
	 * Codigo original: http://stackoverflow.com/a/23674699 Autor: Neeraj
	 * Swarnkar
	 */

	private int resValue = 0;
	private int originalValue = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.number_picker_layout);
		
		// Dar los valores sacandolos del Intent
		originalValue = getIntent().getIntExtra("valor_entero", 0);
		resValue = originalValue;

		/*
		 * Comportamiento del NumberPicker:
		 */
		NumberPicker np = (NumberPicker) findViewById(R.id.number_picker);
		np.setMinValue(0);
		np.setMaxValue(30);
		np.setValue(originalValue);
		np.setWrapSelectorWheel(false);

		np.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

			@Override
			public void onValueChange(NumberPicker picker, int oldVal,
					int newVal) {
				// Actualizar resValue con el nuevo valor:
				resValue = newVal;
			}
		});

		/*
		 * Comportamiento del boton "Aceptar":
		 */
		Button apple_btn = (Button) findViewById(R.id.apply_button);

		apple_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = getIntent();
				i.putExtra("resultado_entero", resValue);
				setResult(RESULT_OK, i);
				finish();
			}
		});

		/*
		 * Comportamiento del boton "Cancelar":
		 */
		Button cancel_btn = (Button) findViewById(R.id.cancel_button);

		cancel_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent i = getIntent();
				i.putExtra("resultado_entero", originalValue);
				setResult(RESULT_OK, i);
				finish();
			}
		});
	}

}